package com.example.springboot.controller;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.Mapper.SpUserMapper;
import com.example.springboot.UserSerive.MenuService;
import com.example.springboot.UserSerive.RoleService;
import com.example.springboot.UserSerive.UserService;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.pojo.InUser;
import com.example.springboot.pojo.Menu;
import com.example.springboot.pojo.Role;
import com.example.springboot.pojo.SpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class MenuController {
    //new SpUserMapper
//    @Autowired
//    private SpUserMapper spUserMapper;
    @Autowired
    private MenuService menuService;
    //查
    @GetMapping
    public List<Menu> index(){
        List<Menu> list=menuService.list();
        //一级菜单
        List<Menu> menuFirst = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        //子菜单
        for (Menu menu:menuFirst) {
           menu.setChildren(list.stream().filter(m->menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return menuFirst;
    }
    //新增与更新
    @PostMapping
    public boolean saveUser(@RequestBody Menu menu){
        //return spUserMapper.in(spUser);
        return menuService.saveOrUpdate(menu);//plus
    }
    //删
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        //return spUserMapper.deleteId(id);
        return menuService.removeById(id);
    }
    //多删除  /{id}只能有一个参数
    @DeleteMapping("/all")
    public boolean deleteAll(@RequestBody List<Integer> ids){
        //return spUserMapper.deleteId(id);
        return menuService.removeByIds(ids);
    }

    //分页


//    @GetMapping("/page")
//    public  Map<String,Object> Page(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String username){
//        pageNum=(pageNum-1)*pageSize;
//        username="%"+username+"%";
//        List<SpUser> data=spUserMapper.selectPage(pageNum,pageSize,username);
//        Integer total=spUserMapper.selectToal();
//        Map<String,Object> res=new HashMap<>();
//        res.put("data",data);
//        res.put("total",total);
//       return res;
//    }

//分页plus

    @GetMapping("/page")
    public  IPage<Menu> Page(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String name){

        IPage<Menu> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Menu> queryWrapper=new QueryWrapper<>();
        if(!"".equals(name)){queryWrapper.like("name",name);}
        queryWrapper.orderByDesc("id");
        return menuService.page(page,queryWrapper);
    }










}
